package com.power.web.action.stage;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ActionContext;
import com.power.Constants;
import com.power.model.aboutus.TsAboutus;
import com.power.model.dataDictionary.TddData;
import com.power.model.department.TiDepartment;
import com.power.model.district.TsDistrict;
import com.power.model.doctor.TiDoctors;
import com.power.model.group.TiGroup;
import com.power.model.hospital.TiHospital;
import com.power.model.user.TsSysuser;
import com.power.services.aboutus.TsAboutusService;
import com.power.services.dataDictionary.DataDictionaryService;
import com.power.services.department.DepartmentService;
import com.power.services.district.TsDistrictService;
import com.power.services.doctors.TiDoctorsService;
import com.power.services.group.TiGroupService;
import com.power.services.hospital.HospitalService;
import com.power.util.CharacterUtil;
import com.power.util.PageUtil;
import com.power.util.RequestUtil;
import com.power.web.action.base.AbstractBaseAction;

public class StageAction extends AbstractBaseAction {
	private static final long serialVersionUID = -2783702669835367117L;

	private String keyword;
	private PageUtil pu;
	private int pageNo;
	private TiHospital hospital;
	private TiDepartment department;
	private TiGroup group;
	private TiDoctors doctor;

	private String jsonString;

	@Autowired
	private HospitalService hosService;

	@Autowired
	private DepartmentService departService;

	@Autowired
	private TsDistrictService districtService;

	@Autowired
	private TiGroupService groupService;

	@Autowired
	private TiDoctorsService docService;

	@Autowired
	private DataDictionaryService dataDictionaryService;
	@Autowired
    @Qualifier("tsAboutusService")
    private TsAboutusService tsAboutusService;
    
    private TsAboutus tsAboutus;
	/**
	 * 搜索医院
	 * 
	 * @return
	 */
	public String searchHospital() {
		try {
			keyword = CharacterUtil.encode(keyword);
			if(pageNo < 1)
				pageNo = 1;
			if(limit < 1)
				limit = 20;
			pu = hosService.searchHos(keyword, pageNo, limit);
		} catch (Exception e) {
			log.error("检索医院出错", e);
		}
		return "hos_result";
	}

	/**
	 * 科室搜索
	 * 
	 * @return
	 */
	public String searchDept() {
		try {
			if(pageNo < 1)
				pageNo = 1;
			if(limit < 1)
				limit = 20;
			keyword = CharacterUtil.encode(keyword);
			pu = departService.searchDept(keyword, pageNo, limit);
		} catch (Exception e) {
			log.error("检索医院出错", e);
		}
		return "dept_result";
	}

	/**
	 * 小组搜索
	 * 
	 * @return
	 */
	public String searchGroup() {
		try {
			if(pageNo < 1)
				pageNo = 1;
			if(limit < 1)
				limit = 20;
			keyword = CharacterUtil.encode(keyword);
			pu = groupService.searchGroup(keyword, pageNo, limit);
		} catch (Exception e) {
			log.error("检索医院出错", e);
		}
		return "group_result";
	}

	/**
	 * 医生搜索
	 * 
	 * @return
	 */
	public String searchDoctor() {
		try {
			if(pageNo < 1)
				pageNo = 1;
			if(limit < 1)
				limit = 20;
			keyword = CharacterUtil.encode(keyword);
			pu = docService.searchDoctor(keyword, pageNo, limit);
		} catch (Exception e) {
			log.error("检索医院出错", e);
		}
		return "doctor_result";
	}

	public String showHosAll() {
		try {
			Map<String, List<TsDistrict>> disMap = districtService.findAllDis();
			ActionContext.getContext().put("disMap", disMap);
		} catch (Exception e) {
			log.error("跳转医院大全出错", e);
		}
		return "showHosAll";
	}

	public String showHosByArea() {
		Integer areaId = RequestUtil.getParam(request, "areaId", -1);
		String areaName = RequestUtil.getParam(request, "aname", "");
		try {
			areaName = CharacterUtil.encode(areaName);
			List<TiHospital> hosList = hosService.findByAreaId(areaId);
			ActionContext.getContext().put("hosList", hosList);
			ActionContext.getContext().put("areaName", areaName);
		} catch (Exception e) {
			log.error("根据地区检索出相应的医院出错", e);
		}
		return "hosArae";
	}

	public String showHospital() {
		Integer hid = RequestUtil.getParam(request, "hid", -1);
		try {
			hospital = hosService.read(hid);
			List<TiDepartment> departList = departService.findByHospitalId(hid);
			ActionContext.getContext().put("departList", departList);
		} catch (Exception e) {
			log.error("显示医院出错", e);
		}
		return "showHospital";
	}

	public String showDepartMent() {
		Integer did = RequestUtil.getParam(request, "did", -1);
		try {
			department = departService.read(did);
			hospital = hosService.read(department.getHid());
			List<TiDepartment> departList = departService
					.findByHospitalId(department.getHid());
			ActionContext.getContext().put("departList", departList);
			List<TiGroup> groupList = groupService.findByDepartId(did);
			ActionContext.getContext().put("groupList", groupList);
		} catch (Exception e) {
			log.error("显示科室出错", e);
		}
		return "showDepartMent";
	}

	public String showGroup() {
		Integer gid = RequestUtil.getParam(request, "gid", -1);
		try {
			group = groupService.read(gid);
			department = departService.read(group.getDid());
			hospital = hosService.read(group.getHid());
			List<TiGroup> groupList = groupService.findByDepartId(group
					.getDid());
			ActionContext.getContext().put("groupList", groupList);
			List<TiDoctors> doctList = docService.findByGroupId(gid);
			ActionContext.getContext().put("doctList", doctList);
		} catch (Exception e) {
			log.error("显示小组出错", e);
		}
		return "showGroup";
	}

	public String showDoctor() {
		Integer docId = RequestUtil.getParam(request, "docId", -1);
		try {
			List<TddData> workTypelist = dataDictionaryService
					.findAllDataByDDID(6);
			doctor = docService.read(docId);
			doctor.setDlevelName(findDataNameByCode(workTypelist, doctor
					.getDlevel()));
			group = groupService.read(doctor.getGid());
			department = departService.read(doctor.getDid());
			hospital = hosService.read(doctor.getHid());
			List<TiDoctors> doctList = docService
					.findByGroupId(doctor.getGid());
			ActionContext.getContext().put("doctList", doctList);
		} catch (Exception e) {
			log.error("展示医生出错", e);
		}
		return "showDoctor";
	}

	private String findDataNameByCode(List<TddData> list, String code) {
		if (list == null || list.size() == 0 || code == null || "".equals(code))
			return "";
		for (TddData tdd : list) {
			if (code.equals(tdd.getCode())) {
				return tdd.getName();
			}
		}
		return "";
	}

	public String searchSuggest() {
		String keyword = RequestUtil.getParam(request, "keyword", "");
		Integer type = RequestUtil.getParam(request, "type", 1);
		List list = null;
		StringBuffer dataStr = new StringBuffer("{\"data\":[");
		try {
//			keyword = CharacterUtil.encode(keyword);
			if (1 == type)
				list = hosService.findBySearchKeyWord(keyword);
			else if (2 == type)
				list = departService.findBySearchKeyWord(keyword);
			else if (3 == type)
				list = groupService.findBySearchKeyWord(keyword);
			else if (4 == type)
				list = docService.findBySearchKeyWord(keyword);
			if (null != list) {
				for (int i = 0; i < list.size(); i++) {
					if(i > 0)
						dataStr.append(",");
					switch (type) {
					case 1:
						TiHospital hos = (TiHospital) list.get(i);
						dataStr.append("{\"title\":\"" + hos.getName() + "\"}");
						break;
					case 2:
						TiDepartment dep = (TiDepartment) list.get(i);
						dataStr.append("{\"title\":\"" + dep.getName() + "\"}");
						break;
					case 3:
						TiGroup group = (TiGroup) list.get(i);
						dataStr.append("{\"title\":\"" + group.getName()
								+ "\"}");
						break;
					case 4:
						TiDoctors doc = (TiDoctors) list.get(i);
						dataStr.append("{\"title\":\"" + doc.getRealName()
								+ "\"}");
						break;
					}
				}
			}
		} catch (Exception e) {
			log.error("助记搜索失败",e);
		}
		dataStr.append("]}");
		jsonString = dataStr.toString();
		return Constants.JSONSTRING_RESULT_NAME;
	}

	public String checkSession() {
		TsSysuser user = (TsSysuser) this.session.get(Constants.USERINFO_KEY);
		String type = "2";
		PrintWriter out = null;
		try {
			out = response.getWriter();
			if (null != user && user.getId() > 0 && "1".equals(user.getCategory())) {
				type = "1";
			}
		} catch (Exception e) {
			log.error("", e);
		}
		out.print(type);
		return null;
	}

	public String toCase() {
		TsSysuser user = (TsSysuser) this.session.get(Constants.USERINFO_KEY);
		String type = "2";
		try {
			if (null != user && user.getId() > 0 && "2".equals(user.getCategory())) {
				type = "1";
			}
		} catch (Exception e) {
			log.error("", e);
		}
		if ("2".equals(type)) {
			return "relogin";
		} else {
			return "tocase";
		}
	}
	
	public String toDy() {
		TsSysuser user = (TsSysuser) this.session.get(Constants.USERINFO_KEY);
		String type = "2";
		try {
			if (null != user && user.getId() > 0 && "2".equals(user.getCategory())) {
				type = "1";
			}
		} catch (Exception e) {
			log.error("", e);
		}
		if ("2".equals(type)) {
			return "relogin";
		} else {
			return "tody";
		}
	}

	public String toZx() {
		TsSysuser user = (TsSysuser) this.session.get(Constants.USERINFO_KEY);
		String type = "2";
		Integer groupId = RequestUtil.getParam(request, "groupId", -1);
		try {
			if (null != user && user.getId() > 0 && "2".equals(user.getCategory())) {
				this.request.getSession().setAttribute("groupId", groupId);
				type = "1";
			}
		} catch (Exception e) {
			log.error("", e);
		}
		if ("2".equals(type)) {
			return "relogin";
		} else {
			return "toZx";
		}
	}
	
	public String dyHelp() {
		try{
			tsAboutus = this.tsAboutusService.read(3);
		}catch (Exception e) {
			log.error("显示导医帮助出错", e);
		}
		return "toDyHelp";
	}
	
	public String ctHelp() {
		try{
			tsAboutus = this.tsAboutusService.read(2);
		}catch (Exception e) {
			log.error("显示ct帮助出错", e);
		}
		return "ctHelp";
	}
	
	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public PageUtil getPu() {
		return pu;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public void setPu(PageUtil pu) {
		this.pu = pu;
	}

	public TiHospital getHospital() {
		return hospital;
	}

	public void setHospital(TiHospital hospital) {
		this.hospital = hospital;
	}

	public TiDepartment getDepartment() {
		return department;
	}

	public void setDepartment(TiDepartment department) {
		this.department = department;
	}

	public TiGroup getGroup() {
		return group;
	}

	public void setGroup(TiGroup group) {
		this.group = group;
	}

	public TiDoctors getDoctor() {
		return doctor;
	}

	public void setDoctor(TiDoctors doctor) {
		this.doctor = doctor;
	}

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	public TsAboutus getTsAboutus() {
		return tsAboutus;
	}

	public void setTsAboutus(TsAboutus tsAboutus) {
		this.tsAboutus = tsAboutus;
	}

}
